package com.wf.smartcomm.server.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.wf.smartcomm.server.bean.Complain;
import com.wf.smartcomm.server.bean.Feedback;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.ComplainService;
import com.wf.smartcomm.server.service.FeedbackService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.StringUtil;


@Controller(value="adminComplainAction")
@Scope("prototype")
public class ComplainAction extends BaseAction {
	@Resource
	private ComplainService complainServiceImpl;
	
	@Resource
	private FeedbackService feedbackServiceImpl;
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		
/*  63 */     page.setBeginIndex(Integer.parseInt(iDisplayStart));
/*  64 */     page.setPageSize(Integer.parseInt(iDisplayLength));
/*  65 */     User manager = (User)this.session.getAttribute("loginUser");
/*  66 */     if (!StringUtil.isEmpty(sSearch))
/*  67 */       this.complainServiceImpl.listByKey(manager.getLoginVillage(), sSearch, page);
/*     */     else {
/*  69 */       this.complainServiceImpl.list(manager.getLoginVillage(), page);
/*     */     }
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
/*     */   public String detail() throws Exception {
/*  83 */     String complainId = this.request.getParameter("complainId");
/*  84 */     Complain complain = this.complainServiceImpl.getById(complainId);
/*  85 */     this.feedbackServiceImpl.listByComplain(complain, this.page);
/* 102 */     this.request.setAttribute("complain", complain);
/* 103 */     if (this.page.getTotalCount() > 0) {
/* 113 */       this.request.setAttribute("feedbacks", page.getDataList());
/*     */     } 
/*     */ 
/* 118 */     return "detail";
/*     */   }
	
/*     */   public String proFeedback() throws Exception {
/* 122 */     Complain complain = complainServiceImpl.getById(request.getParameter("complainId"));
		      feedbackServiceImpl.listByComplain(complain, this.page);
				
				List<Feedback> feedbacks = (List<Feedback>) page.getDataList();
				if (feedbacks == null) {
					feedbacks = new ArrayList<Feedback>();
				}
				
				Feedback feedback = new Feedback();
/* 123 */     	String content = this.request.getParameter("content");
				
/* 125 */     User proUser = (User)this.session.getAttribute("loginUser");

/* 128 */     feedback.setUser(proUser);
/* 129 */     feedback.setComplain(complain);
/* 130 */     feedback.setContent(content);
/* 131 */     this.feedbackServiceImpl.save(feedback);
/* 132 */     this.complainServiceImpl.update(complain);

				feedbacks.add(feedback);
				request.setAttribute("feedbacks", feedbacks);
/* 133 */     return "detail";
/*     */   }
}
